// pages/notice/list.js
var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 15,
    records:[],
    record_tels:[],
    tels: [],
    is_pop:false,
    is_choose_tel:false
  },
  
// 关闭弹窗
  close_pop:function(){
    var record_tels=this.data.info;
    for (var i = 0; i < record_tels.phone.length; i++) {

        record_tels.phone[i].is_choose=false
  
    }
 
    this.setData({
      info: record_tels,
      record_tels: record_tels
    })

    if(this.data.is_pop){
      var list=this.data.list;
      
      this.setData({
        is_pop:false,
        tels: [],
        records: [],
        record_tels: [],
        is_download: false
      })
    }else{
      this.setData({
        is_pop: true
      })
    }
  },

  // 选择下载到通讯录
  fn_choose:function(e){
    this.setData({
        is_pop:true,
      is_choose_tel:true,
      })
  },
  fn_down: function(e) {
    this.setData({
      is_pop: true,
      is_choose_tel: false,
    })
  },
  
  //选择同学通讯录电话
  fn_record_tel:function(e){
    var id = e.currentTarget.dataset.id, is_choose = e.currentTarget.dataset.is_choose,
      index = e.currentTarget.dataset.index,
      records = this.data.info,
      record_tels = this.data.record_tels;
  
    if (is_choose == false) {
      if (record_tels.length<2){
        records.phone[index].is_choose = true;
        record_tels.push(records.phone[index]);
        this.setData({
          info: records,
          record_tels: record_tels,
        })
      }else{
        wx.showToast({
          title: '最多只能选两个电话！',
          icon: 'none',
        })
      }
    
    } else {
      for (var i = 0; i < record_tels.length; i++) {
        console.log(i,record_tels[i].phone,id);
        if (record_tels[i].phone == id) {
          record_tels.splice(i, 1)
        }
      }
      records.phone[index].is_choose = false;
      this.setData({
        info: records,
        record_tels: record_tels
      })
    }
  },
// 点击下载
  go_download: function (e) {
    var info=this.data.info;
    var record_tels = this.data.record_tels;
 
    if (record_tels.length>1) {
        wx.addPhoneContact({
          firstName: info.name,//联系人姓名
          mobilePhoneNumber: record_tels[0].phone,//联系人手机号
          homePhoneNumber: record_tels[1].phone,//住宅电话
          title: info.post,//职位
        })
      } else {
        wx.addPhoneContact({
          firstName: info.name,//联系人姓名
          mobilePhoneNumber: record_tels[0].phone,//联系人手机号
          title: info.post,//职位
        })
      };
    var new_info = this.data.info;
    new_info.phone = this.data.info_tel;
      this.setData({
        record_tels:[],
        info: new_info,
        is_pop: false,
      })
  },
  //电话选择
  gophone: function (e) {
    this.setData({
      is_pop: true,
      is_choose_tel:true,
      is_download:false
    })
  },

  //打电话并产生通话记录
  call_tel: function (e) {
    var phone = e.currentTarget.dataset.phone;
    var id = e.currentTarget.dataset.id;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: (res) => {
        app.ajax(api.add_call_records, {
          phone: phone,
          phone_number_id:id,
          receiver_id: this.data.info.id,
          rd_session: wx.getStorageSync('rd_session'),
        }, (res, err) => {
          this.get_data( this.data.id);
        })
        //  把电话发送给后台产生通话记录
      },
    })
  },
 get_data:function(id){
   app.ajax(api.call_records_detail, {
     id:id,
     rd_session: wx.getStorageSync('rd_session'),
   }, (res, err) => {
     this.setData({
       info: res.data.list
     })
   })
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      member_type: wx.getStorageInfoSync("member_type"),
      id: options.id
    })
    app.ajax(api.call_records_detail, {
      id: options.id,
      rd_session: wx.getStorageSync('rd_session'),
    }, (res, err) => {
      this.setData({
        info: res.data.list,
        info_tel: res.data.list.phone,
      })
    })
 
 
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})